﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Input;
using System.Windows.Threading;

using NetExp.MediaXenter.Utilities;
using NetExp.MediaXenter.Database;
using MediaXenterWPF.UserControl;

namespace MediaXenterWPF.UserControl
{
	public partial class PaginaPrincipale
    {
        private DispatcherTimer timer;
        CircularList<SettingsDS.PulsantiRow> pulsanti;
        CircularList<int> contatore;
        private SettingsDS _settingsDS = null;
        private KeyboardManager _keyboardManager;
        private ThemesResources _themesResources;
        private StyleHelper _styleHelper;
        public event Delegates.OnPrincipaleAnimazioneEnd OnPrincipaleAnimazioneEnd;
        private Key previousKey = Key.A;
        public System.Windows.Size Size
        {
            get
            {
                return new System.Windows.Size(brdContenitore.Width, brdContenitore.Height);
            }
        }
        private System.Windows.Size _size;
        public PaginaPrincipale(double actualWidth, double actualHeight, SettingsDS settingsDS, KeyboardManager keyboardManager, ThemesResources themesResources, StyleHelper styleHelper)
		{
            this.InitializeComponent();
            _settingsDS = settingsDS;
            _keyboardManager = keyboardManager;
            _themesResources = themesResources;
            _styleHelper = styleHelper;
            _size = new System.Windows.Size(actualWidth, actualHeight);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            lblDataOra.Content = DateTime.Now.ToString("dddd dd MMMM yyyy, HH:mm");
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(20);
            timer.Tick += new EventHandler(timer_Tick);

            timer.Start();
            lblTitoloPagina.Content = LocalizationHelper.TranslateString(_settingsDS, lblTitoloPagina.Content.ToString());
            LoadPulsantiera();
            areaSuperiore.Width = _size.Width;
            areaCentrale.Width = _size.Width;
            areaInferiore.Width = _size.Width;
            areaContenitorePulsantiera.Margin = new Thickness(0, 0, 20, 0);

            Thickness thick = new Thickness(0, 0, 0, 0);

            this.Margin = thick;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            lblDataOra.Content = DateTime.Now.ToString("dddd dd MMMM yyyy, HH:mm");
        }


        private void LoadPulsantiera()
        {
            pulsanti = new CircularList<SettingsDS.PulsantiRow>(_settingsDS.Pulsanti.Count);
            contatore = new CircularList<int>(_settingsDS.Pulsanti.Count);

            for (int i = 0; i < _settingsDS.Pulsanti.Count; i++)
            {
                contatore[i] = i;
                
            }
            for (int i = 0; i < _settingsDS.Pulsanti.Count; i++)
            {
                SettingsDS.PulsantiRow riga = _settingsDS.Pulsanti.NewPulsantiRow();
                riga.Nome = _settingsDS.Pulsanti[i].Nome;
                riga.Funzione = _settingsDS.Pulsanti[i].Funzione;
                pulsanti[i] = riga;
            }
            pulsanti.Reset();
            for (int i = 0; i < _settingsDS.Pulsanti.Count; i++)
            {
                Button button = CreaPulsante(pulsanti.Value, i, 0);
                double spazio = -1 * (button.Height + button.Margin.Top + button.Margin.Bottom);
                button = CreaPulsante(pulsanti.Value, i, spazio);

                if (i == Constants.PULSANTIERA_PULSANTE_CENTRALE)
                {
                    imgCentrale.ImageSource = GetImage(button.Content.ToString());
                }

                stkRotazione.Children.Insert(0, button);
                pulsanti.Previous();
            }
            stkRotazione.Height = (((Button)stkRotazione.Children[0]).Height + ((Button)stkRotazione.Children[0]).Margin.Top + ((Button)stkRotazione.Children[0]).Margin.Bottom) * Constants.PULSANTIERA_NUMERO_PULSANTI;
        }

        private Button CreaPulsante(SettingsDS.PulsantiRow pulsante, int indice, double spazio)
        {
            return CreaPulsante(pulsante.Nome, indice, spazio);
        }

        private Button CreaPulsante(string nome, int indice, double spazio)
        {
            TranslateTransform translate = new TranslateTransform(0, spazio);
            Button button = new Button();
            button.Content = string.Empty;
            button.Content = LocalizationHelper.TranslateString(_settingsDS, nome);
            button.Width = 223;
            button.RenderTransformOrigin = new System.Windows.Point(0.5, 0.5);
            button.Margin = new Thickness(0, 10, 0, 10);
            button.RenderTransform = translate;
            button.Height = 50;
            button.FontFamily = _styleHelper.FontNormale.FontFamily;
            button.FontSize = _styleHelper.FontNormale.FontSize;
            button.FontStyle = _styleHelper.FontNormale.Style;
            button.FontWeight = _styleHelper.FontNormale.Weight;
            button.Foreground = new SolidColorBrush(_styleHelper.FontNormale.Color);
            
            if (indice == Constants.PULSANTIERA_PULSANTE_CENTRALE)
            {
                
                button.Style = (Style)FindResource("MediaButtonSelected");
            }
            else
            {
                button.Style = (Style)FindResource("MediaButton");
            }

            return button;
        }

        public void AnimazionePulsanti(Key pulsante)
        {

            DoubleAnimation db = new DoubleAnimation();
            TranslateTransform translate = new TranslateTransform();

            Button p = (Button)stkRotazione.Children[0];
            double spazio = -1 * (p.Height + p.Margin.Top + p.Margin.Bottom);

            if (pulsante == _keyboardManager.MoveUp)
            {
                if (previousKey == Key.A)
                {
                    contatore.Previous();
                    contatore.Previous();
                }
                else if (previousKey == pulsante)
                    contatore.Previous();
    
                pulsanti.Clear();
                for (int i = 0; i < contatore.Value; i++)
                    pulsanti.Previous();

                int j = 0;
                foreach (Button button in stkRotazione.Children)
                {
                    button.Content = LocalizationHelper.TranslateString(_settingsDS, ((SettingsDS.PulsantiRow)pulsanti.Value).Nome);
                    pulsanti.Next();

                    translate = (TranslateTransform)button.RenderTransform;
                    db = new DoubleAnimation(0, spazio, new Duration(TimeSpan.FromTicks(Constants.PULSANTIERA_TEMPO_ANIMAZIONE)));

                    translate.BeginAnimation(TranslateTransform.YProperty, db);
                    if (j == Constants.PULSANTIERA_PULSANTE_CENTRALE + 1)
                    {
                        button.Style = (Style)FindResource("MediaButtonSelected");
                        imgCentrale.ImageSource = GetImage(button.Content.ToString());
                    }
                    else
                    {
                        button.Style = (Style)FindResource("MediaButton");
                    }
                    j += 1;
                }
            }
            else if (pulsante == _keyboardManager.MoveDown)
            {
                if (previousKey == Key.A)
                    contatore.Previous();
                else if (previousKey == pulsante)
                    contatore.Next();
                
                pulsanti.Clear();
                for (int i = 0; i < contatore.Value; i++)
                    pulsanti.Previous();

                int j = 0;
                foreach (Button button in stkRotazione.Children)
                {
                    button.Content = LocalizationHelper.TranslateString(_settingsDS, ((SettingsDS.PulsantiRow)pulsanti.Value).Nome);
                    pulsanti.Next();
                    translate = (TranslateTransform)button.RenderTransform;
                    db = new DoubleAnimation(spazio, 0, new Duration(TimeSpan.FromTicks(Constants.PULSANTIERA_TEMPO_ANIMAZIONE)));

                    translate.BeginAnimation(TranslateTransform.YProperty, db);
                    if (j == Constants.PULSANTIERA_PULSANTE_CENTRALE)
                    {
                        button.Style = (Style)FindResource("MediaButtonSelected");
                        imgCentrale.ImageSource = GetImage(button.Content.ToString());
                    }
                    else
                        button.Style = (Style)FindResource("MediaButton");
                    j += 1;
                
                }
            }
            previousKey = pulsante;

            //if (pulsante == _keyboardManager.MoveUp)
            //{
            //    Button p = (Button)stkRotazione.Children[0];
            //    double spazio = -1 * (p.Height + p.Margin.Top + p.Margin.Bottom);

            //    TranslateTransform t = (TranslateTransform)p.RenderTransform;
            //    if (t.Y == 0)
            //    {
            //        List<string> nomi = new List<string>();
            //        foreach (Button button in stkRotazione.Children)
            //            nomi.Add(button.Content.ToString());

            //        string n = nomi[nomi.Count - 1];
            //        nomi.Remove(n);
            //        nomi.Insert(0, n);
            //        stkRotazione.Children.Clear();
            //        foreach (string nome in nomi)
            //        {
            //            Button button = CreaPulsante(nome, 0, 0);
            //            stkRotazione.Children.Add(button);
            //        }

            //        p = (Button)stkRotazione.Children[0];
            //    }



            //    stkRotazione.Children.Remove(p);
            //    stkRotazione.Children.Add(p);
            //    int i = 0;
            //    foreach (Button button in stkRotazione.Children)
            //    {

            //        if (button.RenderTransform == null)
            //        {
            //            translate = new TranslateTransform(0, 0);
            //            db = new DoubleAnimation(0, 0, new Duration(TimeSpan.FromTicks(Constants.PULSANTIERA_TEMPO_ANIMAZIONE)));
            //            button.RenderTransform = translate;
            //        }
            //        translate = (TranslateTransform)button.RenderTransform;
            //        db = new DoubleAnimation(0, spazio, new Duration(TimeSpan.FromTicks(Constants.PULSANTIERA_TEMPO_ANIMAZIONE)));

            //        translate.BeginAnimation(TranslateTransform.YProperty, db);

            //        if (i == Constants.PULSANTIERA_PULSANTE_CENTRALE + 1)
            //        {
            //            button.Style = (Style)FindResource("MediaButtonSelected");
            //            imgCentrale.ImageSource = GetImage(button.Content.ToString());
            //        }
            //        else
            //        {
            //            button.Style = (Style)FindResource("MediaButton");
            //        }
            //        i += 1;
            //    }
            //}
            //else if (pulsante == _keyboardManager.MoveDown)
            //{
            //    Button p = (Button)stkRotazione.Children[stkRotazione.Children.Count - 1];
            //    double spazio = -1 * (p.Height + p.Margin.Top + p.Margin.Bottom);

            //    TranslateTransform t = (TranslateTransform)p.RenderTransform;
            //    if (t.Y == spazio)
            //    {
            //        List<string> nomi = new List<string>();
            //        foreach (Button button in stkRotazione.Children)
            //            nomi.Add(button.Content.ToString());
            //        string n = nomi[0];
            //        nomi.Remove(n);
            //        nomi.Add(n);
            //        stkRotazione.Children.Clear();
            //        foreach (string nome in nomi)
            //        {
            //            Button button = CreaPulsante(nome, 0, 0);
            //            stkRotazione.Children.Add(button);
            //        }

            //        p = (Button)stkRotazione.Children[stkRotazione.Children.Count - 1];
            //    }
            //    stkRotazione.Children.Remove(p);
            //    stkRotazione.Children.Insert(0, p);
            //    int i = 0;
            //    foreach (Button button in stkRotazione.Children)
            //    {

            //        if (button.RenderTransform == null)
            //        {
            //            translate = new TranslateTransform(0, 0);
            //            db = new DoubleAnimation(0, 0, new Duration(TimeSpan.FromTicks(Constants.PULSANTIERA_TEMPO_ANIMAZIONE)));
            //            button.RenderTransform = translate;
            //        }
            //        translate = (TranslateTransform)button.RenderTransform;

            //        db = new DoubleAnimation(spazio, 0, new Duration(TimeSpan.FromTicks(Constants.PULSANTIERA_TEMPO_ANIMAZIONE)));

            //        translate.BeginAnimation(TranslateTransform.YProperty, db);

            //        if (i == Constants.PULSANTIERA_PULSANTE_CENTRALE)
            //        {
            //            button.Style = (Style)FindResource("MediaButtonSelected");
            //            imgCentrale.ImageSource = GetImage(button.Content.ToString());
            //        }
            //        else
            //            button.Style = (Style)FindResource("MediaButton");
            //        i += 1;
            //    }
            //}

        }

        private ImageSource GetImage(string testo)
        {
            string converted = LocalizationHelper.OriginalString(_settingsDS, testo, Constants.LOCALIZATION_AREA_PAGINA_PRINCIPALE);

            if(converted=="S0101")
                return _themesResources.Video;
            else if(converted=="S0102")
                return _themesResources.Musica;
            else if(converted=="S0103")
                return _themesResources.Foto;
            else if(converted=="S0104")
                return _themesResources.Impostazioni;
            else if(converted=="S0105")
                return _themesResources.Plugin;
            else if(converted=="S0106")
                return _themesResources.Televisione;

            return null;

        }

        internal PagePosition GetSelectedPage()
        {
            PagePosition position = new PagePosition();
            foreach (Button button in stkRotazione.Children)
            {
                if (button.Style == (Style)FindResource("MediaButtonSelected"))
                {
                    string valoreOrig = CompareString(button.Content.ToString());
                    if (valoreOrig == "S0101")
                    {
                        position = PagePosition.Video;
                    }
                    else if (valoreOrig == "S0102")
                    {
                        position = PagePosition.Musica;
                    }
                    else if (valoreOrig == "S0103")
                    {
                        position = PagePosition.Foto;
                    }
                    else if (valoreOrig == "S0104")
                    {
                        position = PagePosition.Impostazioni;
                    }
                    else if (valoreOrig == "S0105")
                    {
                        position = PagePosition.Plugin;
                    }
                    else if (valoreOrig == "S0106")
                    {
                        position = PagePosition.Televisione;
                    }
                    break;
                }
            }
            return position;
        }

        private string CompareString(string nome)
        {
            foreach (SettingsDS.PulsantiRow riga in _settingsDS.Pulsanti)
            {
                string valore = LocalizationHelper.TranslateString(_settingsDS, riga.Nome);
                if (valore == nome)
                    return riga.Nome;
            }
            return nome;
        }

        internal Border AreaAnimazione()
        {
            return brdContenitore;
        }

        internal void ReductionAnimation()
        {
            Duration duration = new Duration(TimeSpan.FromSeconds(1));


            Storyboard sb = new Storyboard();
            sb.Completed += new EventHandler(storyBoard1_Completed);
            sb.Duration = duration;
            sb.Children.Add(AnimationHelper.AnimationScaleY(duration, brdContenitore, 1, 0));
            sb.Children.Add(AnimationHelper.AnimationScaleX(duration, brdContenitore, 1, 0));
            sb.Children.Add(AnimationHelper.AnimationTranslateTransformY(duration, brdContenitore, 0, -200));
            sb.Children.Add(AnimationHelper.AnimationScaleY(duration, areaImmagine, 1, 0));
            sb.Children.Add(AnimationHelper.AnimationScaleX(duration, areaImmagine, 1, 0));
            sb.Children.Add(AnimationHelper.AnimationTranslateTransformY(duration, areaImmagine, 0, -200));
            sb.Begin();
        }

        void storyBoard1_Completed(object sender, EventArgs e)
        {
            if (OnPrincipaleAnimazioneEnd != null)
                OnPrincipaleAnimazioneEnd();

        }

        internal void ZoomAnimation()
        {
            Duration duration = new Duration(TimeSpan.FromSeconds(1));

            Storyboard sb = new Storyboard();

            sb.Duration = duration;
            sb.Children.Add(AnimationHelper.AnimationScaleY(duration,brdContenitore,0,1));
            sb.Children.Add(AnimationHelper.AnimationScaleX(duration, brdContenitore, 0, 1));
            sb.Children.Add(AnimationHelper.AnimationTranslateTransformY(duration, brdContenitore, -200, 0));
            sb.Children.Add(AnimationHelper.AnimationScaleY(duration, areaImmagine, 0, 1));
            sb.Children.Add(AnimationHelper.AnimationScaleX(duration, areaImmagine, 0, 1));
            sb.Children.Add(AnimationHelper.AnimationTranslateTransformY(duration, areaImmagine, -200, 0));



            sb.Begin();
        }
    }
}